package com.meiyu.live.ui.main;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.meiyu.live.BaseFragment;
import com.meiyu.live.R;
import com.meiyu.live.comm.Constant;
import com.meiyu.live.comm.UserConfig;
import com.meiyu.live.eventbus.FocusEvent;
import com.meiyu.live.eventbus.LoginEvent;
import com.meiyu.live.net.param.GetPageParams;
import com.meiyu.live.net.response.MysubscriptionResponse;
import com.meiyu.live.popj.Hookup;
import com.meiyu.live.ui.login.LoginActivity;
import com.meiyu.live.ui.main.widget.LiveItemView;
import com.meiyu.live.widget.MSGView;

import de.greenrobot.event.EventBus;

/**
 * 我的订阅
 */
public class MySubscribeLiveFragment extends BaseFragment {


    private LayoutInflater inflater;
    private SwipeRefreshLayout refreshLayout;
    private ListView listView;
    private MSGView msgView;
    private DataListAdapter adapter;
    private View footerView;

    private static final int PAGE_SIZE = 10;
    private int index = 1;
    private boolean hasMore = true;
    private MysubscriptionResponse mysubscriptionResponse = new MysubscriptionResponse();

    public MySubscribeLiveFragment() {
    }

    public static MySubscribeLiveFragment newInstance() {
        MySubscribeLiveFragment fragment = new MySubscribeLiveFragment();
        return fragment;
    }

    @Override
    public int getContextView() {
        return R.layout.fragment_hotlive;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    protected void init() {
        inflater = LayoutInflater.from(getActivity());
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData(true);
            }
        });
        listView = (ListView) findViewById(R.id.listview);
        msgView = (MSGView) findViewById(R.id.msgView);
        msgView.dismiss();
        msgView.setErrorClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(LoginActivity.class);
            }
        });

        footerView = inflater.inflate(R.layout.view_loadmore, null);
        listView.addFooterView(footerView);
        footerView.setVisibility(View.GONE);

        adapter = new DataListAdapter();
        listView.setAdapter(adapter);
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int scrollState) {
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) { // 判断滚动到底部
                    if (absListView.getLastVisiblePosition() == (absListView.getCount() - 1) && hasMore) {
                        footerView.setVisibility(View.VISIBLE);
                        getData(false);
                    }
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {

            }
        });

        getData(true);
    }

    private void getData(final boolean reload) {
        if (!UserConfig.getInstance().isLogin()) {
            msgView.show_error("请登录");
            return;
        }
        if (reload) {
            index = 1;
            hasMore = true;
        }

        int area[] = {PAGE_SIZE, (index) * PAGE_SIZE};
        GetPageParams pageParams = new GetPageParams(area);
        pageParams.setUrl(Constant.VERSION + Constant.UrlParams.MYSUBSCRIPTION);
        //热门约播取hot_hook_ups字段的值
        request(pageParams, MysubscriptionResponse.class, new Response.Listener<MysubscriptionResponse>() {
            @Override
            public void onResponse(MysubscriptionResponse hookupsResponse) {
                refreshLayout.setRefreshing(false);
                if (hookupsResponse != null) {
                    if (reload) {
                        MySubscribeLiveFragment.this.mysubscriptionResponse.getHash().clear();
                    }
                    MySubscribeLiveFragment.this.mysubscriptionResponse.getHash().addAll(hookupsResponse.getHash());
                    index += 1;
                    if (hookupsResponse.getHash().size() < PAGE_SIZE) {
                        hasMore = false;
                    }
                    if (mysubscriptionResponse.getHash().size() == 0) {
                        msgView.show_empty();
                    } else {
                        msgView.dismiss();
                    }
                    adapter.notifyDataSetChanged();
                }
                footerView.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public void onErrorResponse(VolleyError volleyError) {
        refreshLayout.setRefreshing(false);
        footerView.setVisibility(View.GONE);
        super.onErrorResponse(volleyError);
    }

    class DataListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mysubscriptionResponse != null && mysubscriptionResponse.getHash() != null ? mysubscriptionResponse.getHash().size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LiveItemView liveItemView = null;
            if (convertView == null) {
                liveItemView = new LiveItemView(getActivity());
                convertView = liveItemView;
            } else {
                liveItemView = (LiveItemView) convertView;
            }
            final Hookup bean = mysubscriptionResponse.getHash().get(position);
            liveItemView.setData(bean);
            return convertView;
        }
    }


    public void onEvent(FocusEvent event) {
        getData(true);
    }

    public void onEvent(LoginEvent event) {
        if (event.isLogin) {
            getData(true);
        } else {
            mysubscriptionResponse.getHash().clear();
            adapter.notifyDataSetChanged();
        }
    }
}
